﻿using System.Collections.Generic;
using System.Data;

namespace ncp.Business
{
    public class GoodsInfoBll
    {
        /// <summary>
        /// 得到分页商品信息表
        /// </summary>
        public static DataTable GetGoodsInfoTable(int piPageIndex, int piPageSize, string pcFieldList, string pcOrderBy, string pcWhere, string p_DataBase)
        {
            string sqlStr = CommonBll.GetPagingSQL(piPageIndex, piPageSize, "bi_GoodsInfo_v", pcFieldList, "g_id", pcOrderBy, pcWhere);
            return CommonBll.GetdtList(sqlStr, p_DataBase);
        }

        /// <summary>
        /// 得到商品信息表
        /// </summary>
        public static DataTable GetGoodsInfoTable(string p_Where, string p_Orderby, string pDataBase)
        {
            string sqlStr = "select * from bi_GoodsInfo_v";
            if (!string.IsNullOrEmpty(p_Where))
                sqlStr += " where " + p_Where;
            if (!string.IsNullOrEmpty(p_Orderby))
                sqlStr += " order by " + p_Orderby;
            return CommonBll.GetdtList(sqlStr, pDataBase);
        }

        /// <summary>
        /// 得到记录数
        /// </summary>
        public static int GetGoodsInfoCount(string pcWhere, string pDataBase)
        {
            string sqlStr = "select count(*) from bi_GoodsInfo";
            if (!string.IsNullOrEmpty(pcWhere))
                sqlStr += " where " + pcWhere;
            return CommonBll.ReadInteger(sqlStr, pDataBase);
        }

        /// <summary>
        /// 修改商品信息
        /// </summary>
        public static int UpdateGoodsInfo(Dictionary<string, object> dicGoods, string pDataBase)
        {
            string sqlStr = "if not exists(select 1 from bi_GoodsInfo where (G_Name='" + dicGoods["g_name"].ToString() + "' and GC_Id = '" + dicGoods["gc_id"].ToString() + "') and G_Id<>'" + dicGoods["g_id"].ToString() + "') " +
                            "UPDATE dbo.bi_GoodsInfo " +
                            "SET GC_Id = '" + dicGoods["gc_id"].ToString() + "'," +
                            "G_Name = '" + dicGoods["g_name"].ToString() + "', " +
                            "G_Code =  '" + dicGoods["g_code"].ToString() + "', " +
                            "G_Flag =  '" + dicGoods["g_flag"].ToString() + "', " +
                            "G_Status =  '" + dicGoods["g_status"].ToString() + "' " +
                            "where G_Id='" + dicGoods["g_id"].ToString() + "'";
            return CommonBll.ExecuteNonQuery(sqlStr, pDataBase);
        }

        /// <summary>
        /// 增加商品信息
        /// </summary>
        public static int InsertGoodsInfo(Dictionary<string, object> dicGoods, string pDataBase)
        {
            string sqlStr = "INSERT INTO dbo.bi_GoodsInfo (GC_Id, G_Name, G_Code, G_Status,G_Flag) " +
                            "VALUES ('" + dicGoods["gc_id"].ToString() + "', '" + dicGoods["g_name"].ToString() + "', '" + dicGoods["g_code"].ToString() + "', '" + dicGoods["g_status"].ToString() + "', '" + dicGoods["g_flag"].ToString() + "')";
            return CommonBll.ExecuteQuery(sqlStr, pDataBase);
        }

        /// <summary>
        /// 删除商品
        /// </summary>
        public static int DeleteGoodsInfo(string G_Id, string pDataBase)
        {
            string sqlStr = "if not exists(select 1 from bi_ProductInfo where G_Id in (" + G_Id + ")) " +
                        "delete from bi_GoodsInfo where G_Id in(" + G_Id + ")";
            return CommonBll.ExecuteNonQuery(sqlStr, pDataBase);
        }
    }
}
